package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.OperationUserLog;

import com.novax.ex.activity.open.api.inner.OperationUserLogInnerApi;
import com.novax.ex.activity.open.model.query.OperationUserLogQuery;
import com.novax.ex.activity.open.model.request.OperationUserLogRequest;
import com.novax.ex.activity.open.model.response.OperationUserLogOuterResponse;
import com.novax.ex.activity.open.model.response.OperationUserLogResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.OperationUserLogService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description 活动落地页配置
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationUserLogInnerController extends BaseActivityController implements OperationUserLogInnerApi {

    @Resource
    private OperationUserLogService operationUserLogService;

    @Override
    public ReturnResult<PageUtil<OperationUserLogResponse>> page(String lanuage, OperationUserLogQuery query) {
        PageUtil<OperationUserLogResponse> pageInfo = new PageUtil<>(query);
        pageInfo.setCondition(Collections.singletonMap("query", query));
        operationUserLogService.findPage(pageInfo, OperationUserLogResponse.class);
        return ReturnResult.success(getInternational(lanuage, InternationalCode.QUERY_SUCCESS), pageInfo);
    }

    @Override
    public ReturnResult<List<OperationUserLogOuterResponse>> queryList(String lanuage, OperationUserLogQuery query) {
        PageUtil<OperationUserLogResponse> pageInfo = new PageUtil<>(query);
        pageInfo.setCondition(Collections.singletonMap("query", query));
        List<OperationUserLog> list = operationUserLogService.queryList(pageInfo);
        if(list.isEmpty()){
            return ReturnResult.success(getInternational(lanuage, InternationalCode.QUERY_SUCCESS), Collections.EMPTY_LIST);
        }
        List<OperationUserLogOuterResponse>dataList = CopyUtils.copyList(list, OperationUserLogOuterResponse.class);
        return ReturnResult.success(getInternational(lanuage, InternationalCode.QUERY_SUCCESS), dataList);
    }

    @Override
    public ReturnResult<?> add(String language, String ip, OperationUserLogRequest operationUserLogRequest,
                               BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        OperationUserLog operationConfig = CopyUtils.copyObject(operationUserLogRequest, OperationUserLog.class);
        operationConfig.setIp(ip);
        operationConfig.setCreateTime(new Date());
        boolean res = operationUserLogService.addSelective(operationConfig);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
